package net.lab1024.sa.admin.module.upstream.product;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.lab1024.sa.admin.module.app.user.domain.UserEntity;
import net.lab1024.sa.admin.module.business.oa.enterprise.dao.EnterpriseDao;
import net.lab1024.sa.admin.module.business.oa.enterprise.domain.vo.EnterpriseVO;
import net.lab1024.sa.admin.module.system.login.domain.RequestEmployee;
import net.lab1024.sa.admin.module.upstream.product.domain.ProductEntity;
import net.lab1024.sa.admin.module.upstream.product.domain.form.ProductAddForm;
import net.lab1024.sa.admin.module.upstream.product.domain.form.ProductQueryForm;
import net.lab1024.sa.admin.module.upstream.product.domain.ProductVO;
import net.lab1024.sa.admin.module.upstream.product.domain.form.ProductUpdateForm;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.enumeration.EmployeeTypeEnum;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductService {

    private final ProductDao productDao;

    private final EnterpriseDao enterpriseDao;

    public ProductService(ProductDao productDao,
                          EnterpriseDao enterpriseDao) {
        this.productDao = productDao;
        this.enterpriseDao = enterpriseDao;
    }

    public PageResult<ProductVO> queryByPage(ProductQueryForm queryForm, RequestEmployee requestUser) {
        Integer employeeType = requestUser.getEmployeeType();
        if (EmployeeTypeEnum.ENTERPRISE.getValue().equals(employeeType)) {
            queryForm.setEnterpriseId(requestUser.getEnterpriseId());
        }
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<ProductVO> list = productDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }

    public ResponseDTO<String> add(ProductAddForm addForm) {
        // 校验产品名称
        ProductEntity entityByName = productDao.queryByProductName(addForm.getProductName());
        if (entityByName != null) {
            return ResponseDTO.userErrorParam("产品名称已存在存在");
        }
        // 校验企业
        EnterpriseVO enterprise = enterpriseDao.getDetail(addForm.getEnterpriseId(), Boolean.FALSE);
        if (enterprise == null) {
            return ResponseDTO.userErrorParam("企业不存在");
        }
        ProductEntity entity = SmartBeanUtil.copy(addForm, ProductEntity.class);
        productDao.insert(entity);
        return ResponseDTO.ok();
    }

    public ResponseDTO<String> update(ProductUpdateForm updateForm) {
        ProductEntity entityById = productDao.selectById(updateForm.getProductId());
        if (entityById == null) {
            return ResponseDTO.userErrorParam("产品不存在存在");
        }
        // 校验产品名称
        ProductEntity entityByName = productDao.queryByProductName(updateForm.getProductName());
        if (entityByName != null && !updateForm.getProductId().equals(entityByName.getProductId())) {
            return ResponseDTO.userErrorParam("产品名称已存在存在");
        }
        // 校验企业
        EnterpriseVO enterprise = enterpriseDao.getDetail(updateForm.getEnterpriseId(), Boolean.FALSE);
        if (enterprise == null) {
            return ResponseDTO.userErrorParam("企业不存在");
        }
        ProductEntity entity = SmartBeanUtil.copy(updateForm, ProductEntity.class);
        productDao.updateById(entity);
        return ResponseDTO.ok();
    }
}
